package com.neusoft.neuiotms.dcom.converter;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.dto.DictionaryDTO;
import com.neusoft.neuiotms.common.service.DictionaryService;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.WorkDailyDTO;
import com.neusoft.neuiotms.dcom.model.WorkDaily;
import com.neusoft.neuiotms.dcom.repository.WorkDailyRepository;
import com.neusoft.neuiotms.dcom.vo.WorkDailyExportVO;
import com.neusoft.neuiotms.dcom.vo.WorkDailyImportVO;

@Component
public class WorkDailyConverter extends AbstractConverter<WorkDaily, WorkDailyDTO> {

    @Autowired
    private WorkDailyRepository workDailyRepository;
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public WorkDaily toModel(final WorkDailyDTO dto) {
        WorkDaily model;
        if (dto.isNew()) {
            model = new WorkDaily();
        } else {
            model = this.workDailyRepository.getOne(dto.getId());
        }

        model.setDailyReportDate(dto.getDailyReportDate());
        model.setUnitCode(dto.getUnitCode());
        model.setUnitName(dto.getUnitName());
        model.setOperationGroupCode(dto.getOperationGroupCode());
        model.setOperationGroupName(dto.getOperationGroupName());
        model.setEventRequester(dto.getEventRequester());
        model.setResponseMethod(dto.getResponseMethod());
        model.setIncidentLocation(dto.getIncidentLocation());
        model.setIncidentUserCode(dto.getIncidentUserCode());
        model.setIncidentUserName(dto.getIncidentUserName());
        model.setServiceTypeCode(dto.getServiceTypeCode());
        model.setServiceTypeName(dto.getServiceTypeName());
        model.setCreateTime(dto.getCreateTime());
        model.setServiceAttributesCode(dto.getServiceAttributesCode());
        model.setServiceAttributesName(dto.getServiceAttributesName());

        final Calendar c = Calendar.getInstance();
        c.setTime(dto.getServiceStartTime());
        c.set(Calendar.YEAR, 1970);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.SECOND, 0);
        model.setServiceStartTime(c.getTime());

        c.setTime(dto.getServiceEndTime());
        c.set(Calendar.YEAR, 1970);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.SECOND, 0);
        model.setServiceEndTime(c.getTime());

        // 保证开始时间和结束时间别搞错了
        if (model.getServiceStartTime().after(model.getServiceEndTime())) {
            final Date dateStart = model.getServiceStartTime();
            model.setServiceStartTime(model.getServiceEndTime());
            model.setServiceEndTime(dateStart);
        }

        c.setTime(model.getServiceStartTime());
        final long s = c.getTimeInMillis();
        c.setTime(model.getServiceEndTime());
        final long e = c.getTimeInMillis();
        model.setTotalWorkingHours(Math.abs(((e - s) / 60000) * 1.0));

        model.setEventLevel(dto.getEventLevel());
        model.setTrackingNo(dto.getTrackingNo());
        model.setJobContent(dto.getJobContent());
        model.setRemarks(dto.getRemarks());

        model.setAttachment(dto.getAttachment());

        return model;
    }

    @Override
    public WorkDailyDTO toDTO(final WorkDaily model, final boolean forListView) {

        final WorkDailyDTO dto = new WorkDailyDTO();

        dto.setId(model.getId());
        dto.setDailyReportDate(model.getDailyReportDate());
        dto.setUnitCode(model.getUnitCode());
        dto.setUnitName(model.getUnitName());
        dto.setOperationGroupCode(model.getOperationGroupCode());
        dto.setOperationGroupName(model.getOperationGroupName());
        dto.setEventRequester(model.getEventRequester());
        dto.setResponseMethod(model.getResponseMethod());
        if (!forListView) {
            final DictionaryDTO dict =
                    this.dictionaryService.findByDictionaryTypeAndCode("responseMethod", model.getResponseMethod());
            if (null != dict) {
                dto.setResponseMethodText(dict.getText());
            }
        }
        dto.setIncidentLocation(model.getIncidentLocation());
        dto.setIncidentUserCode(model.getIncidentUserCode());
        dto.setIncidentUserName(model.getIncidentUserName());
        dto.setServiceTypeCode(model.getServiceTypeCode());
        dto.setServiceTypeName(model.getServiceTypeName());
        dto.setCreateTime(model.getCreateTime());
        dto.setServiceAttributesCode(model.getServiceAttributesCode());
        dto.setServiceAttributesName(model.getServiceAttributesName());
        dto.setServiceStartTime(model.getServiceStartTime());
        dto.setServiceEndTime(model.getServiceEndTime());
        dto.setEventLevel(model.getEventLevel());
        if (!forListView) {
            final DictionaryDTO dict =
                    this.dictionaryService.findByDictionaryTypeAndCode("eventgrade", model.getEventLevel());
            if (null != dict) {
                dto.setEventLevelText(dict.getText());
            }
        }
        dto.setTotalWorkingHours(model.getTotalWorkingHours());
        dto.setTrackingNo(model.getTrackingNo());
        dto.setJobContent(model.getJobContent());
        dto.setRemarks(model.getRemarks());
        dto.setAttachment(model.getAttachment());

        return dto;
    }

    public WorkDailyExportVO toVO(final WorkDaily model) {
        final WorkDailyExportVO vo = new WorkDailyExportVO();
        if (model.getDailyReportDate() != null) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(model.getDailyReportDate());
            vo.setYear(calendar.get(Calendar.YEAR));
            vo.setMonth(calendar.get(Calendar.MONTH) + 1);
            vo.setDay(calendar.get(Calendar.DATE));
            vo.setWeek(calendar.get(Calendar.DAY_OF_WEEK));
        }
        vo.setEventLevel(model.getEventLevel());
        vo.setEventRequester(model.getEventRequester());
        vo.setIncidentLocation(model.getIncidentLocation());
        vo.setIncidentUserName(model.getIncidentUserName());
        vo.setJobContent(model.getJobContent());
        vo.setOperationGroupName(model.getOperationGroupName());
        vo.setRemarks(model.getRemarks());
        vo.setResponseMethod(model.getResponseMethod());
        vo.setServiceAttributesName(model.getServiceAttributesName());
        vo.setServiceEndTime(model.getServiceEndTime());
        vo.setServiceStartTime(model.getServiceStartTime());
        vo.setServiceTypeName(model.getServiceTypeName());
        vo.setTotalWorkingHours(model.getTotalWorkingHours());
        vo.setTrackingNo(model.getTrackingNo());
        vo.setUnitName(model.getUnitName());
        return vo;
    }

    public WorkDaily toModel(final WorkDailyImportVO vo) {
        final WorkDaily model = new WorkDaily();
        model.setDailyReportDate(vo.getDailyReportDate());
        if (StringUtils.isNotBlank(vo.getEventLevel())) {
            model.setEventLevel(vo.getEventLevel().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        }
        model.setEventRequester(vo.getEventRequester());
        model.setIncidentLocation(vo.getIncidentLocation());
        model.setIncidentUserCode(vo.getIncidentUser().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setIncidentUserName(vo.getIncidentUser().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        model.setJobContent(vo.getJobContent());
        model.setOperationGroupCode(vo.getOperationGroup().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setOperationGroupName(vo.getOperationGroup().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        model.setRemarks(vo.getRemarks());
        model.setResponseMethod(vo.getResponseMethod());
        if (StringUtils.isNotBlank(vo.getServiceAttributes())) {
            model.setServiceAttributesCode(vo.getServiceAttributes().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
            model.setServiceAttributesName(vo.getServiceAttributes().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        }
        if (StringUtils.isNotBlank(vo.getServiceType())) {
            model.setServiceTypeCode(vo.getServiceType().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
            model.setServiceTypeName(vo.getServiceType().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        }

        final Calendar c = Calendar.getInstance();
        c.setTime(vo.getServiceStartTime());
        c.set(Calendar.YEAR, 1970);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 2);
        c.set(Calendar.SECOND, 0);
        model.setServiceStartTime(c.getTime());

        c.setTime(vo.getServiceEndTime());
        c.set(Calendar.YEAR, 1970);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 2);
        c.set(Calendar.SECOND, 0);
        model.setServiceEndTime(c.getTime());

        // 保证开始时间和结束时间别搞错了
        if (model.getServiceStartTime().after(model.getServiceEndTime())) {
            final Date dateStart = model.getServiceStartTime();
            model.setServiceStartTime(model.getServiceEndTime());
            model.setServiceEndTime(dateStart);
        }

        c.setTime(model.getServiceStartTime());
        final long s = c.getTimeInMillis();
        c.setTime(model.getServiceEndTime());
        final long e = c.getTimeInMillis();
        model.setTotalWorkingHours(Math.abs(((e - s) / 60000) * 1.0));

        model.setTrackingNo(vo.getTrackingNo());
        model.setUnitCode(vo.getUnit().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setUnitName(vo.getUnit().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        return model;
    }
}
